package com.free.plan.service;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.free.plan.mapper.DevPlanMapper;
import com.free.plan.domain.DevPlan;

/**
 * 开发计划Service业务层处理
 *
 * @author free
 * @date 2024-02-08
 */
@Service
public class DevPlanService extends ServiceImpl<DevPlanMapper, DevPlan> {
    @Autowired
    private DevPlanMapper devPlanMapper;

    /**
     * 查询开发计划
     *
     * @param id 开发计划主键
     * @return 开发计划
     */
    public DevPlan selectDevPlanById(Long id) {
        return devPlanMapper.selectById(id);
    }

    /**
     * 查询开发计划列表
     *
     * @param devPlan 开发计划
     * @return 开发计划
     */
    public List<DevPlan> selectDevPlanList(DevPlan devPlan) {

        QueryWrapper<DevPlan> wrapper = new QueryWrapper<>();

        wrapper.eq(ObjectUtils.isNotEmpty(devPlan.getId()), "id", devPlan.getId());
        wrapper.like(ObjectUtils.isNotEmpty(devPlan.getTitle()), "title", devPlan.getTitle());
        wrapper.eq(ObjectUtils.isNotEmpty(devPlan.getStatus()), "status", devPlan.getStatus());
        wrapper.eq(ObjectUtils.isNotEmpty(devPlan.getBeginTime()), "begin_time", devPlan.getBeginTime());
        wrapper.eq(ObjectUtils.isNotEmpty(devPlan.getCompleteTime()), "complete_time", devPlan.getCompleteTime());
        wrapper.orderByDesc("create_time");
        return devPlanMapper.selectList(wrapper);
    }

    /**
     * 新增开发计划
     *
     * @param devPlan 开发计划
     * @return 结果
     */
    public int insertDevPlan(DevPlan devPlan) {
        return devPlanMapper.insert(devPlan);
    }

    /**
     * 修改开发计划
     *
     * @param devPlan 开发计划
     * @return 结果
     */
    public int updateDevPlan(DevPlan devPlan) {
        if (ObjectUtil.equals(devPlan.getStatus(), 122)) {
            devPlan.setBeginTime(LocalDateTime.now());
        }
        if (ObjectUtil.equals(devPlan.getStatus(), 123)) {
            devPlan.setCompleteTime(LocalDateTime.now());
        }

        return devPlanMapper.updateById(devPlan);
    }

    /**
     * 批量删除开发计划
     *
     * @param ids 需要删除的开发计划主键
     * @return 结果
     */
    public int deleteDevPlanByIds(List<Long> ids) {
        return devPlanMapper.deleteBatchIds(ids);
    }

    /**
     * 删除开发计划信息
     *
     * @param id 开发计划主键
     * @return 结果
     */
    public int deleteDevPlanById(Long id) {
        return devPlanMapper.deleteById(id);
    }
}
